using System;
using ValidationFramework;

namespace ExampleLibraryCSharp.CustomRuleAttributeExample
{

    public enum CardType
    {
        Visa,
        MasterCard,
        Amex
    }
    public class Person
    {

        #region Properties
        [RequiredEnumRule]
        public CardType? CardType
        {
            get;
            set;
        }

		[CustomRule("ExampleLibraryCSharp.CustomRuleAttributeExample.Person,ExampleLibraryCSharp", "CheckValidCreditCard",
      "Validate based on credit card rules.", ErrorMessage = "Credit card is invalid.")]
        public string CreditCardNumber
        {
            get;
            set;
        }


        #endregion


        #region Methods

        private static void CheckValidCreditCard(object sender, CustomValidationEventArgs e)
        {
            var person = (Person)e.TargetObjectValue;
            var cardType = person.CardType;
            var cardNumber = person.CreditCardNumber;
            if (cardNumber == null || cardType == null)
            {
                e.IsValid = false;
            }
            else
            {
                switch (cardType.Value)
                {
					case CustomRuleAttributeExample.CardType.Visa:
					case CustomRuleAttributeExample.CardType.MasterCard:
                        {
                            e.IsValid = (cardNumber.Length == 16);
                            break;
                        }
					case CustomRuleAttributeExample.CardType.Amex:
                        {
                            e.IsValid = (cardNumber.Length == 15);
                            break;
                        }
                    default:
                        {
                            throw new ArgumentException("Invalid CardType");
                        }
                }
            }
        }

        #endregion

    }
}